/* 
 * File:   Xmsg.cpp
 * Author: minaev
 * 
 * Created on 10 Октябрь 2013 г., 12:55
 */

#include "Xmsg.h"
#include "Xservice.h"
#include "oustream.h"

#include <iostream>
#include <stdio.h>
#include <ostream>
#include <iosfwd>
#include <stdarg.h>

char** Xmsg::bufStrs = new char*[17];
pthread_mutex_t Xmsg::mprt;
char*  Xmsg::msg = new char[MAX_LEN_PRINTF];

Xmsg::Xmsg() {
}

Xmsg::~Xmsg() {
}

/**
 * Logging single string
 * @param msg - single string
 */
void Xmsg::log(const char* msg) {
    std::cerr << msg << "\n";
}

/**
 * Logging multiple strings
 * @param n - count logging strings
 * @param ... - strings
 */
void Xmsg::log(int n, ...) {
    if (n > 16) {
        n = 16;
    }
    va_list strs;
    va_start(strs, n);
    int i = 0;
    while (i < n) {
        Xmsg::bufStrs[i] = va_arg(strs, char*);
        i++;
    }
    Xmsg::bufStrs[i] = NULL;
    va_end(strs);
    log(Xservice::toStr(Xmsg::bufStrs));
}

int Xmsg::printf(__const char *__restrict __format, ...) {
    /*char* msg = new char[MAX_LEN_PRINTF];*/
    pthread_mutex_lock(&Xmsg::mprt);
    va_list args;
    va_start(args, __format);
    vsnprintf(msg, MAX_LEN_PRINTF, __format, args);
    std::cout << msg;
    pthread_mutex_unlock(&Xmsg::mprt);
    //delete [] msg;
}